#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import threading
import time
from tkinter import messagebox

from selenium import webdriver
import selenium.webdriver.support.select

from auto import Constant, Properties


def get_jenkins_properties(env):
    if env == Constant.PRD:
        return Properties.get_j_u_prd(), Properties.get_j_n_prd(), Properties.get_j_p_prd()
    elif env == Constant.RC:
        return Properties.get_j_u_rc(), Properties.get_j_n_rc(), Properties.get_j_p_rc()
    else:
        return Properties.get_j_u_dev(), Properties.get_j_n_dev(), Properties.get_j_p_dev()


def get_driver(env):
    url, name, password = get_jenkins_properties(env)
    chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
    os.environ["webdriver.chrome.driver"] = chrome_driver
    driver = webdriver.Chrome(chrome_driver)
    driver.get(url)
    time.sleep(1)
    driver.find_element_by_id("j_username").send_keys(name)
    driver.find_element_by_name("j_password").send_keys(password)
    driver.find_element_by_id("remember_me").click()
    driver.find_element_by_id("yui-gen1-button").click()
    return driver


def do_pkg_web(branch_name, project_name):
    driver = get_driver(Constant.DEV)
    root_url = Properties.get_j_u_dev()
    url = root_url + "view/打包-web2.0/job/build-" + project_name + "/"
    driver.get(url + "ws")
    driver.find_element_by_class_name("subtasks").find_element_by_class_name("task-link").click()
    time.sleep(3)  # 延时5秒，用于手动选择
    # driver.switch_to.alert.dismiss()  # 弹窗提示：dismiss()-取消、accept()-确定
    driver.get(url + "/build?delay=0sec")
    time.sleep(2)
    selenium.webdriver.support.select.Select(driver.find_element_by_id("select")).select_by_value(branch_name)


def do_pkg_service(branch_name, project_name):
    url = Properties.get_j_u_dev()
    driver = get_driver(Constant.DEV)
    driver.get(url + "view/打包-service2.0/job/build-service/ws")
    driver.find_element_by_class_name("subtasks").find_element_by_class_name("task-link").click()
    time.sleep(5)
    # driver.switch_to.alert.dismiss()
    driver.get(url + "view/打包-service2.0/job/build-service/build?delay=0sec")
    time.sleep(2)
    selenium.webdriver.support.select.Select(driver.find_element_by_id("select")).select_by_value(branch_name)
    selenium.webdriver.support.select.Select(driver.find_elements_by_class_name("setting-main")[1].find_element_by_name("parameter").find_element_by_name("value")).select_by_value(project_name)


def do_pkg_info(branch_name):
    url = Properties.get_j_u_dev()
    driver = get_driver(Constant.DEV)
    driver.get(url + "view/打包-service2.0/job/build-info/ws")
    time.sleep(1)
    driver.find_element_by_class_name("subtasks").find_element_by_class_name("task-link").click()
    time.sleep(5)
    # driver.switch_to.alert.dismiss()
    driver.get(url + "view/打包-service2.0/job/build-info/build?delay=0sec")
    time.sleep(2)
    selenium.webdriver.support.select.Select(driver.find_element_by_id("select")).select_by_value(branch_name)


def do_pkg_job(branch_name):
    url = Properties.get_j_u_dev()
    driver = get_driver(Constant.DEV)
    driver.get(url + "view/打包-service2.0/job/build-service-job/ws")
    time.sleep(1)
    driver.find_element_by_class_name("subtasks").find_element_by_class_name("task-link").click()
    time.sleep(5)
    # driver.switch_to.alert.dismiss()
    driver.get(url + "view/打包-service2.0/job/build-service-job/build?delay=0sec")
    time.sleep(2)
    selenium.webdriver.support.select.Select(driver.find_element_by_id("select")).select_by_value(branch_name)


def deploy_trade_dev():
    url = Properties.get_j_u_dev()
    driver = get_driver(Constant.DEV)
    driver.get(url + "view/WEB3.0_部署/job/trade-web-v3-deploy(git)/build?delay=0sec")


def deploy_provider_dev():
    url = Properties.get_j_u_dev()
    driver = get_driver(Constant.DEV)
    driver.get(url + "view/SRV3.0_部署/job/service-provider-v3-deploy(git)/build?delay=0sec")


def deploy_trade_rc():
    url = Properties.get_j_u_rc()
    driver = get_driver(Constant.RC)
    driver.get(url + "view/RC环境web服务/job/RC--trade-web-git/build?delay=0sec")


def deploy_provider_rc():
    url = Properties.get_j_u_rc()
    driver = get_driver(Constant.RC)
    driver.get(url + "view/RC环境service服务/job/RC--service-provider-git/build?delay=0sec")


def deploy_trade_prd():
    url = Properties.get_j_u_prd()
    driver = get_driver(Constant.PRD)
    driver.get(url + "view/生产环境web服务/job/PRD--trade-web-git/build?delay=0sec")


def deploy_provider_prd():
    url = Properties.get_j_u_prd()
    driver = get_driver(Constant.PRD)
    driver.get(url + "view/生产环境service服务/job/PRD--service-provider-git/build?delay=0sec")


def start_package(branch, project):
    if project in Constant.WEB_PKG_LIST:
        threading.Thread(target=do_pkg_web, args=(branch, project)).start()
    elif project in Constant.SERVICE_PKG_LIST:
        threading.Thread(target=do_pkg_service, args=(branch, project)).start()
    elif project == Constant.SVC_INFO:
        threading.Thread(target=do_pkg_info, args=(branch,)).start()
    elif project == Constant.SVC_JOB:
        threading.Thread(target=do_pkg_job, args=(branch,)).start()
    else:
        messagebox.showwarning(title='温馨提示', message='【' + project + '】项目暂不支持此操作！')


def start_deploy_dev(project):
    if project == Constant.TRADE:
        threading.Thread(target=deploy_trade_dev, args=()).start()
    elif project == Constant.PROVIDER:
        threading.Thread(target=deploy_provider_dev, args=()).start()
    else:
        messagebox.showwarning(title='温馨提示', message='【' + project + '】项目暂不支持此操作！')


def start_deploy_rc(project):
    if project == Constant.TRADE:
        threading.Thread(target=deploy_trade_rc, args=()).start()
    elif project == Constant.PROVIDER:
        threading.Thread(target=deploy_provider_rc, args=()).start()
    else:
        messagebox.showwarning(title='温馨提示', message='【' + project + '】项目暂不支持此操作！')


def start_deploy_prd(project):
    if project == Constant.TRADE:
        threading.Thread(target=deploy_trade_prd, args=()).start()
    elif project == Constant.PROVIDER:
        threading.Thread(target=deploy_provider_prd, args=()).start()
    else:
        messagebox.showwarning(title='温馨提示', message='【' + project + '】项目暂不支持此操作！')

